import listCard from '../../../components/acessory/listCard'
import { dateFormat, dayDiff } from '@/utils/formatDate.js'

export default {
  components: {
    listCard,
  },

  data() {
    return {
      info: null,
      id: '',
      showRecommend: false, //是否显示推荐寻配件列表
      recommendList: [], //推荐寻配件列表
      showPopup: false, //弹窗
      expressXuan: 1, //物流标记 1物流 2 快递
      expressData: {},
      remainder: '', //询价有效期剩余时间
      remainderQuote: '', //报价有效期剩余时间
      logisticsInput: {
        color: '#333',
        fontSize: '26rpx',
      },
      logisticsCompany: '', //物流公司
      logisticsNo: '', //物流单号
      popup: {
        show: false,
        title: '',
        btnText: '',
        mode: 'middle',
        val: '',
      },
      pageFrom: '', //从哪里进入详情
    }
  },
  computed: {
    statusIcon() {
      return this.$store.state.accessory.quoteOrderStatusIcon
    },
    status() {
      return this.$store.state.accessory.quoteOrderStatus
    },
    quality() {
      return this.$store.state.accessory.quality
    },
    userName() {
      return this.$store.state.userInfoDetail.user.userName
    },
    userPhone() {
      return this.$store.state.user.userInfo.mobile
    },
    storeId() {
      return this.$store.state.userInfoDetail.store.storeId
    },
  },

  onLoad(e) {
    this.id = e.id
    this.pageFrom = e.pageFrom ? e.pageFrom : ''
    this.getRecommendList()
    this.getexpresscompany()
  },

  onShow() {
    this.getInfo()
  },

  methods: {
    dateFormat,
    dayDiff,

    getInfo() {
      uni.showLoading({
        title: '加载中',
      })
      this.$api.accessory
        .orderDetail({
          order_parts_id: this.id,
        })
        .then((res) => {
          this.info = res.data
          if (res.data.status === 0) {
            this.remainder = this.dayDiff(this.info.effective_time)
            this.remainderQuote = this.dayDiff(
              this.info.snapshootlist[0].effective_time_offer
            )
          }
          uni.hideLoading()
        })
        .catch((e) => {
          console.log(e)
          uni.hideLoading()
        })
    },

    //获取推荐询价
    getRecommendList() {
      this.$api.accessory
        .getRecommendList({
          pageIndex: 1,
          pageSize: 8,
          lat: this.$store.state.latitude
            ? this.$store.state.latitude
            : 104.063364,
          lon: this.$store.state.longitude
            ? this.$store.state.longitude
            : 30.566,
        })
        .then((res) => {
          console.log(res)
          if (res.data.list) {
            this.recommendList = res.data.list
            this.showRecommend = true
          }
        })
        .catch((e) => {})
    },

    //获取快递信息
    getexpresscompany() {
      this.$api.jsonData
        .getexpresscompany()
        .then((res) => {
          console.log(res)
          this.expressData = res.data
        })
        .catch((e) => {
          console.log(e)
        })
    },

    //选择物流公司
    selExpress(data) {
      this.showPopup = false
      this.logisticsCompany = data.name
    },

    // 订单操作弹窗 取消报价 确认物流信息 删除
    handleOrder(val) {
      if (val === 'del') {
        this.popup.show = true
        this.popup.title = '请确认您是否删除此订单'
        this.popup.btnText = '确认删除'
        this.popup.val = val
      } else if (val === 'getGoods') {
        if (!this.logisticsCompany || !this.logisticsNo) {
          uni.showToast({
            title: '请录入物流信息',
            icon: 'none',
          })
          return
        }
        this.popup.show = true
        this.popup.title = '请确认您已发货？'
        this.popup.btnText = '确认发货'
        this.popup.val = val
      } else if (val === 'cancel') {
        this.popup.show = true
        this.popup.title = '请确认您是否取消此订单'
        this.popup.btnText = '确认取消'
        this.popup.val = val
      }
    },

    popupFn() {
      if (this.popup.val === 'del') {
        this.deleteFn()
      } else if (this.popup.val === 'getGoods') {
        this.confirmExpress()
      } else if (this.popup.val === 'cancel') {
        this.cancelFn()
      }
    },

    //取消报价
    cancelFn() {
      this.$api.accessory
        .cancelOrder({
          id: this.id,
          store_id: this.storeId,
        })
        .then((res) => {
          this.popup.show = false
          if (res.data) {
            uni.showToast({
              title: '订单已取消',
              duration: 2000,
              icon: 'success',
            })
          } else {
            uni.showToast({
              title: '订单取消异常，请稍后重试',
              icon: 'none',
              duration: 2000,
            })
          }
          setTimeout(() => {
            this.getInfo()
          }, 2000)
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    //确认物流信息
    confirmExpress() {
      this.$api.accessory
        .setLogistics({
          id: this.id,
          store_id: this.storeId,
          logistics_company_name: this.logisticsCompany,
          logistics_no: this.logisticsNo,
        })
        .then((res) => {
          this.popup.show = false
          if (res.data) {
            uni.showToast({
              title: '物流信息提交成功',
              icon: 'success',
              duration: 2000,
            })
          } else {
            uni.showToast({
              title: '物流信息提交异常，请稍后重试',
              icon: 'none',
              duration: 2000,
            })
          }
          setTimeout(() => {
            this.getInfo()
          })
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    //删除
    deleteFn() {
      this.$api.accessory
        .deleteOrder({
          id: this.id,
          store_id: this.storeId,
        })
        .then((res) => {
          this.popup.show = false
          if (res.data) {
            uni.showToast({
              title: '订单已删除',
              duration: 2000,
              icon: 'success',
            })
          } else {
            uni.showToast({
              title: '订单删除异常，请稍后重试',
              icon: 'none',
              duration: 2000,
            })
          }
          setTimeout(() => {
            uni.navigateBack()
          }, 2000)
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    //打电话
    call() {
      uni.makePhoneCall({
        phoneNumber: this.info.sell_phone,
      })
    },

    //联系
    chat() {
      if (this.pageFrom === 'chat') {
        uni.navigateBack()
        return
      }
      let carPartDetailsStr = ''
      for (let i = 0; i < this.info.snapshootlist.length; i++) {
        if (i > 4) {
          break
        }
        carPartDetailsStr =
          carPartDetailsStr + this.info.snapshootlist[i].parts_name + ' '
      }
      this.$store.commit('news/createConversationActive', {
        user: this.info.store_buy_name, //店铺名称
        img: this.info.store_buy_topimg, //店铺头像
        lastTime: '', //
        userId: this.info.store_buy_id, //店铺id
        conversationID: 'C2C' + this.info.store_buy_id,
      })
      this.$store.commit('news/setCardInfo', {
        imgSrc: 'https://files.yzsheng.com' + this.info.brand_img,
        name: this.info.car_name,
        carPartDetailsStr: carPartDetailsStr,
        type: 'accessoryOrder',
      })
      uni.navigateTo({
        url:
          '/pages_news/contactSeller/contactSeller?user=' +
          this.info.store_buy_name +
          '&pageId=' +
          this.info.id +
          '&tab=1',
      })
    },

    //图片处理
    handleImg(imgStr) {
      return JSON.parse(imgStr)
    },

    //前往报价人列表
    quoteMember() {
      uni.navigateTo({
        url:
          '/pages_findAccessory/detailsInqParticipation/detailsInqParticipation?type=1&id=' +
          this.info.car_part_id,
      })
    },
  },
}
